#include "Plugin.h"
#include <QDebug>
#include <QString>
#include <QMainWindow>
#include <QSize>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGLWidget>
#include <QApplication>
#include <QDesktopWidget>


QString MediaPlayerPlugin::initiate()
{
	qDebug() << "Initiating MediaPlayer plugin";

	// set up graphics view and scene housing video output
	//QGraphicsScene *scene = new QGraphicsScene();
	//QGraphicsView *view = new QGraphicsView(scene);
	//view->setViewport(new QGLWidget); // for optimization


	// set up media object
	media = new Phonon::MediaObject();

	// set up video
	videoWidget = new Phonon::VideoWidget();
	Phonon::createPath(media, videoWidget);
	//view->setFixedSize(QSize(1024,768));
	//view->showFullScreen();
	videoWidget->setFixedSize(QSize(400,300));
	videoWidget->move(1280-400,0);
	videoWidget->show();
	//videoWidget->setFullScreen(true);
	//scene->addWidget(videoWidget);

	// set up audio
	audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory);
	Phonon::createPath(media, audioOutput);

	media->setCurrentSource(QString("/Users/niklas/Movies/scrubs s07e05 klipp.mov"));
	media->play();


	//scene->addLine(50,50,700,700,QPen());



	qDebug() << media->errorString();

	return "";
}


SRCommandList MediaPlayerPlugin::doCommand(SRCommand c)
{
	qDebug() << "media player plugin executing command " << c.getName();

	if(c.getName().compare("media_open") == 0)
	{
		media->setCurrentSource(c.getArgument(0));
		media->play();
	}
	else if(c.getName().compare("media_pause") == 0)
	{
		media->pause();
	}
	else if(c.getName().compare("media_play") == 0)
	{
		media->play();
	}

	return SRCommandList();
}


QString MediaPlayerPlugin::destroy()
{
	qDebug() << "Destroying MediaPlayer plugin";

	delete media;
	delete videoWidget;
	delete audioOutput;

	return "";
}


QString MediaPlayerPlugin::getName() { return "MediaPlayer"; }

QString MediaPlayerPlugin::getVersion()	{ return "0.1"; }



Q_EXPORT_PLUGIN2(mediaplayerplugin, MediaPlayerPlugin);
